﻿/// <reference path="jquery-2.0.3.js" />
/// <reference path="class.js" />
var controls = controls || {};
(function (c) {
	var TableView = Class.create({
		init: function (cols, itemsSource) {
			if (!(itemsSource instanceof Array)) {
				throw "The itemsSource of a TableView must be an array!";
			}

			if (cols == 0) {
			    throw "Cols must be greater than 0";
			}

			this.cols = cols;
			this.itemsSource = itemsSource;
		},
		render: function (template) {
		    var elementTable = document.createElement("table");
		    var rows = this.itemsSource.length / this.cols;
		    for (var row = 0; row < rows; row++) {
		        var currentRow = document.createElement("tr");

		        for (var col = 0; col < this.cols; col++) {
		            var currentCol = document.createElement("td");
		            currentCol.innerHTML = template(this.itemsSource[row+col]);
		            currentRow.appendChild(currentCol);
		        }

		        elementTable.appendChild(currentRow);
		    }

		    return elementTable.outerHTML;
		}
	});

	c.getTableView = function (cols, itemsSource) {
	    return new TableView(cols, itemsSource);
	}
}(controls || {}));